package main;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class TimeHelper {
    public static String getFormattedNowString() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = now.format(formatter);
        return time;
    }

    public static String getFromattedCurrentYear() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
        String year = now.format(formatter);
        return year;
    }

    public static String getFormattedYear2Year() {
        return "2019 - " + TimeHelper.getFromattedCurrentYear();
    }

    /**
     * 测试参数给定的时间是否在给定的小时数之前
     * @param timestamp
     * @param hours
     * @return
     */
    public static boolean isTimeNHoursBefore(Timestamp timestamp, int hours) {
        Timestamp now = new Timestamp(new Date().getTime());
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(now.getTime());
        cal.add(Calendar.HOUR, -hours);
        Timestamp timeBeforeNHours = new Timestamp(cal.getTime().getTime());
        boolean before = timestamp.before(timeBeforeNHours);
        return before;
    }

    /**
     * 测试参数给定的时间是否在给定的分钟之前
     * @param timestamp
     * @param minutes
     * @return
     */
    public static boolean isTimeNMinutesBefore(Timestamp timestamp, int minutes) {
        Timestamp now = new Timestamp(new Date().getTime());
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(now.getTime());
        cal.add(Calendar.MINUTE, -minutes);
        Timestamp timeBeforeNMinutes = new Timestamp(cal.getTime().getTime());
        boolean before = timestamp.before(timeBeforeNMinutes);
        return before;
    }


    /**
     * 测试参数给定的时间是否在给定参数的周数内
     * @param timestamp，给定的时间参数
     * @param weeks，给定的周数
     * @return
     */
    public static boolean isTimeWithinWeeks(Timestamp timestamp, int weeks) {
        Timestamp now = new Timestamp(new Date().getTime());
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(now.getTime());
        cal.add(Calendar.HOUR, -168 * weeks);

        Timestamp timeBeforeNWeeks = new Timestamp(cal.getTime().getTime());
        boolean within = timestamp.after(timeBeforeNWeeks);
        return within;
    }
}
